package com.lwq.im.chartdemo.handler;

import com.alibaba.fastjson.JSON;
import com.lwq.im.chartdemo.bean.RequestBody;
import com.lwq.im.chartdemo.constants.Constants;
import com.lwq.im.chartdemo.session.OnlineSession;
import io.netty.channel.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


/**
 * 绑定时间
 */
@Component
@Slf4j
public class BindHandler implements ChartRequestHandler {



    @Autowired
    private OnlineSession onlineSession;
    /**
     * 绑定事件标识
     *
     * @return
     */
    @Override
    public String getOpType(){
        return Constants.OpType.OP_TYPE_BIND;
    }


    /**
     * 处理绑定事件 业务
     *
     * @param channel
     * @param requestBody
     */
    @Override
    public void doHandle(Channel channel, RequestBody requestBody) {
        log.info("处理绑定事件。通道={},requestBody={} ",channel.id().asShortText(), JSON.toJSONString(requestBody));
        String userId = requestBody.getUserId();
        String deviceId = requestBody.getDeviceId();
        channel.attr(Constants.ChannelAttrKye.USER_ID).set(userId);
        channel.attr(Constants.ChannelAttrKye.DEVICE_ID).set(deviceId);
        onlineSession.put(channel);

        //  TODO 这里可以 向客户端会写，告诉客户端，已经收到绑定消息，并绑定成功
        // channel.writeAndFlush("");

    }
}
